package com.example.zwx.myapplication;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/1/21.
 */
public class AppMsg {
    private static Toast mToast;
    private static int duration = Toast.LENGTH_SHORT;
    private static int gravity = Gravity.BOTTOM;
    private static Handler mHandler = new Handler(Looper.getMainLooper());

    public static void showToast(final Context context, final String msg, int duration, int gravity) {

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                TextView textView;
                if (mToast == null) {
                    mToast = new Toast(context);
                    textView = new TextView(context);
                    textView.setTextSize(18);
                    textView.setGravity(Gravity.CENTER);
                    textView.setBackgroundColor(context.getResources().getColor(android.R.color.background_dark));
                    textView.setTextColor(context.getResources().getColor(android.R.color.white));
                    textView.setPadding(16, 16, 16, 16);
                    mToast.setView(textView);

                } else {
                    textView = (TextView) mToast.getView();
                }
                textView.setText(msg);
                mToast.show();
            }
        });

    }

    public static void showToast(Context context, String msg) {
        showToast(context, msg, duration, Gravity.CENTER);
    }
}
